package cn.changein.framework.service.other.impl;

import cn.changein.framework.constant.OtherConstants;
import cn.changein.framework.kernel.model.response.ResponseData;
import cn.changein.framework.model.other.CheckUpdateDTO;
import cn.changein.framework.service.other.OtherService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;

/**

 */
@Service
@Slf4j
public class OtherServiceImpl implements OtherService {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Override
    public List<String> getHideIconText() {
        String str = stringRedisTemplate.opsForValue().get(OtherConstants.HIDE_ICON_TEXTS);
        if (StringUtils.isBlank(str)) {
            return Lists.newArrayList();
        }
        return Arrays.asList(str.split(","));
    }

    @Override
    public ResponseData configUpdate(CheckUpdateDTO checkUpdateDTO) {
        if (CollectionUtils.isEmpty(checkUpdateDTO.getChanges())) {
            return ResponseData.error("本次更新内容不能为空");
        }
        if (StringUtils.isBlank(checkUpdateDTO.getLastVersion())) {
            return ResponseData.error("更新的版本号不能为空");
        }
        if (StringUtils.isBlank(checkUpdateDTO.getDownloadUrl())) {
            return ResponseData.error("更新的跳转地址不能为空");
        }
        stringRedisTemplate.opsForValue().set(OtherConstants.UPDATE_CONFIG, JSONObject.toJSONString(checkUpdateDTO));
        return ResponseData.success();
    }

    @Override
    public ResponseData getUpdateConfig(CheckUpdateDTO checkUpdateDTO) {
        String str = stringRedisTemplate.opsForValue().get(OtherConstants.UPDATE_CONFIG);
        if (StringUtils.isBlank(str)) {
            return null;
        }
        return ResponseData.success(JSONObject.parseObject(str, CheckUpdateDTO.class));
    }
}
